﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.OleDb;
using System.IO;
using Cocbook_DTO;
using Cocbook_DAL;

namespace Sale_and_manage
{
    public partial class frmBookReport : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmBookReport()
        {
            InitializeComponent();
            LoadProject();
            LoadDatatable((new ProjectDAL()).GetProjectByStatus().ProjectID);
        }

        public List<BookReportView> lb = new List<BookReportView>();
        public List<Project> lp = new List<Project>();

        private void btnGetFilelocation_Click(object sender, EventArgs e)
        {
            
        }

        public void ExportFileExcel(string FileLocation)
        {
            string fileLocation = saveFileDialog1.FileName;
            OleDbConnection connection = new OleDbConnection();
            File.Copy(@"BookReport.xls", fileLocation, true);

            connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties = \"Excel 8.0;HDR=Yes;\";");
            OleDbCommand cmd;
            string excelQuery = @"INSERT INTO [Sheet1$]([TT], [Mã], [Tên], [DVT], [Giá bìa], [Đơn vị], [Tổng nhập],	[Tổng bán],	[Tồn lý thuyết]) " +
                                "VALUES (@tt, @isbn, @title, @type, @price, @issuer, @import, @sold, @inventory)"; ;
            connection.Open();
            progressBar1.Maximum = lb.Count - 1;
            progressBar1.Minimum = 0;
            int status = 0;
            for (int i = 0; i < lb.Count; i++)
            {
                cmd = new OleDbCommand(excelQuery, connection);
                cmd.Parameters.AddWithValue("tt", (i + 1).ToString());
                cmd.Parameters.AddWithValue("isbn", lb[i].ISBN.ToString());
                cmd.Parameters.AddWithValue("title", lb[i].Title.ToString());
                cmd.Parameters.AddWithValue("type", lb[i].Type.ToString());
                cmd.Parameters.AddWithValue("issuer", lb[i].Issuer.ToString());
                cmd.Parameters.AddWithValue("price", lb[i].Price.ToString());
                cmd.Parameters.AddWithValue("import",lb[i].Import.ToString());
                cmd.Parameters.AddWithValue("sold",lb[i].Sold.ToString());
                cmd.Parameters.AddWithValue("inventory", lb[i].Inventory.ToString());
                cmd.ExecuteNonQuery();
                progressBar1.Value = i;
                if ((i+1) * 100 / lb.Count > status)
                {
                    status = (i+1) * 100 / lb.Count;
                    lblStatus.Text = status.ToString() + "%";
                    lblStatus.Refresh();
                }
            }
            connection.Close();
            MessageBox.Show("Hoàn tất");

        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Title = "Save as...";
            saveFileDialog1.FileName = "Cocbook - Báo cáo kho";
            saveFileDialog1.Filter = "Excel 93-2003 Workbook|*.xls";
            if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                btnExport.Enabled = false;
                btnRefresh.Enabled = false;
                ExportFileExcel(saveFileDialog1.FileName);
                btnExport.Enabled = true ;
                btnRefresh.Enabled = true;
            }
        }

        public void LoadDatatable(int pid)
        {
            lb = (new Project_BookDAL()).GetBookReportByProjectID(pid);
            dgvBookReport.DataSource = lb;
        }

        public int GetProjectID(int i)
        {
            return lp[i].ProjectID;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (cboProjectName.SelectedIndex >= 0)
            {
                LoadDatatable(GetProjectID(cboProjectName.SelectedIndex));
            }
        }

        public void LoadProject()
        {
            lp = (new ProjectDAL()).GetAllProject();
            if (lp != null)
            {
                for (int i = 0; i < lp.Count; i++)
                {
                    if (lp[i].ProjectName == "Order")
                    {
                        lp.RemoveAt(i);
                        i = i - 1;
                    }
                    else
                    {
                        cboProjectName.Items.Add(lp[i].ProjectName);
                    }
                }
            }
        }

        private void frmBookReport_Load(object sender, EventArgs e)
        {
        }

    }
}